//
//  GHGridLayout.m
//  collectionView实现瀑布流
//
//  Created by Hello Cai on 2018/7/27.
//  Copyright © 2018年 Hello Cai. All rights reserved.
// 说明：自定义布局，非流水布局，继承自最原始的布局UICollectionViewLayout

#import "GHGridLayout.h"

@interface GHGridLayout ()
/** 所有的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
@end

@implementation GHGridLayout

-(NSMutableArray *)attrsArray{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

//用来做布局的初始化操作
-(void)prepareLayout{
    [super prepareLayout];
    
}


-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    //打印结果：null。最原始的布局(UICollectionViewLayout)不会提供array，不像流水布局(UICollectionViewFlowLayout)算好了array。故我们需要自己提供排布
//    NSArray *array = [super layoutAttributesForElementsInRect:rect];
//    NSLog(@"array:%@",array);
//    return array;
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i=0; i<count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        /*实现类似流水布局的布局
        //设置布局属性
        CGFloat width = self.collectionView.frame.size.width/2;
        CGFloat height = width;
        attrs.frame = CGRectMake(i%2*width, i/2*height, width, height);
        //添加布局属性
        [self.attrsArray addObject:attrs];
         */
        
        CGFloat width = self.collectionView.frame.size.width * 0.5;
        if (i==0) {
            CGFloat height = width;
            CGFloat x = 0;
            CGFloat y = 0;
            attrs.frame = CGRectMake(x, y, width, height);
        }else if (i==1){
            CGFloat height = width*0.5;
            CGFloat x = width;
            CGFloat y = 0;
            attrs.frame = CGRectMake(x, y, width, height);
        }else if (i==2){
            CGFloat height = width*0.5;
            CGFloat x = width;
            CGFloat y = height;
            attrs.frame = CGRectMake(x, y, width, height);
        }else if (i==3) {
            CGFloat height = width*0.5;
            CGFloat x = 0;
            CGFloat y = width;
            attrs.frame = CGRectMake(x, y, width, height);
        }else if (i==4){
            CGFloat height = width*0.5;
            CGFloat x = 0;
            CGFloat y = height+width;
            attrs.frame = CGRectMake(x, y, width, height);
        }else if (i==5){
            CGFloat height = width;
            CGFloat x = width;
            CGFloat y = height;
            attrs.frame = CGRectMake(x, y, width, height);
        }else{
            UICollectionViewLayoutAttributes *preAttrs = self.attrsArray[i-6];
            CGRect lastFrame = preAttrs.frame;
            lastFrame.origin.y = lastFrame.origin.y + width*2;
            attrs.frame = lastFrame;
        }
        [self.attrsArray addObject:attrs];
    }
    return self.attrsArray;
}

/*
 *返回collectionView的内容大小
 */
-(CGSize)collectionViewContentSize{
    //重点是高度
    /*这里用最后一个cell的最大Y正好显示不全，处理一下
    NSArray *array = [self layoutAttributesForElementsInRect:CGRectZero];
    UICollectionViewLayoutAttributes *lastAttrs = [array lastObject];
    
    CGFloat h = CGRectGetMaxY(lastAttrs.frame);
     return CGSizeMake(0, h);
    */
    
    int count = (int)[self.collectionView numberOfItemsInSection:0];
    int rows = (count + 3 - 1) / 3;
    CGFloat rowH = self.collectionView.frame.size.width * 0.5;
    return CGSizeMake(0, rows * rowH);
}
@end
